package net.biancheng.controller;

import java.util.List;

import net.biancheng.bean.SysUser;
import net.biancheng.common.annotation.Log;
import net.biancheng.common.core.BaseController;
import net.biancheng.common.core.CommonResult;
import net.biancheng.common.core.page.TableDataInfo;
import net.biancheng.common.enums.BusinessType;
import net.biancheng.common.utils.SecurityUtils;
import net.biancheng.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户Controller
 * 
 * @author lb
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController
{

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询系统用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        return getDataTable(list);
    }

    /**
     * 获取系统用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable Long id)
    {
        return CommonResult.success(sysUserService.selectSysUserById(id));
    }

    /**
     * 新增保存系统用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "系统用户", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody SysUser sysUser)
    {
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        sysUser.setCreateBy(SecurityUtils.getUsername());//用户注册时无法获取登录用户
        return toResult(sysUserService.insertSysUser(sysUser));
    }

    /**
     * 修改保存系统用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "系统用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody SysUser sysUser)
    {
        return toResult(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除系统用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "系统用户", businessType = BusinessType.DELETE)
    @DeleteMapping( "/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toResult(sysUserService.deleteSysUserByIds(ids));
    }
}